package cn.vcjf.washcar.server.controller;


import cn.vcjf.washcar.common.web.TableResult;
import cn.vcjf.washcar.server.entity.Feedback;
import cn.vcjf.washcar.server.service.FeedbackService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 用户意见反馈表 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-07-18
 */
@Controller
@RequestMapping("/server/feedback")
public class FeedbackController {
	@Resource
	private FeedbackService feedbackService;

	@RequestMapping("/addFeedback")
	@ResponseBody
	public TableResult addFeedback(Feedback feedback) {
		feedback.preInsert();
		boolean b = feedbackService.insert(feedback);
		return TableResult.Result(b);
	}

	@RequestMapping("/updateFeedback")
	@ResponseBody
	public TableResult updateFeedback(Feedback feedback) {
		feedback.preUpdate();
		boolean b = feedbackService.updateById(feedback);
		return TableResult.Result(b);
	}

	@RequestMapping("/findFeedbackOne")
	@ResponseBody
	public TableResult findFeedbackOne(Feedback feedback) {

		EntityWrapper<Feedback> wrapper = feedback.getQueryWrapper();

		Feedback result = feedbackService.selectOne(wrapper);

		return TableResult.Result(result);
	}

	@RequestMapping("/findFeedbackList")
	@ResponseBody
	public TableResult findFeedbackList(Feedback feedback) {

		EntityWrapper<Feedback> wrapper = new EntityWrapper<Feedback>();
		//Todo
		/*wrapper.eq("id",feedback.getId());*/
		if (StringUtils.isNotBlank(feedback.getPhone())) {
			wrapper.eq("u.phone", feedback.getPhone());
		}
		if (StringUtils.isNotBlank(feedback.getCommitUser())) {
			wrapper.eq("u.name", feedback.getCommitUser());
		}
		Page<Feedback> page = new Page<Feedback>(feedback.getPage(), feedback.getLimit());

		page = feedbackService.selectPage(page, wrapper);

		return TableResult.Result(page);

	}

	@RequestMapping("/deleteFeedbackByIds")
	@ResponseBody
	public TableResult deleteFeedbackByIds(String... ids) {
		List<String> list = Arrays.asList(ids);
		boolean b = feedbackService.deleteBatchIds(list);
		return TableResult.Result(b);
	}
}
